package com.java.system.common.rest;

import com.github.pagehelper.PageInfo;
import com.java.system.common.msg.RestRes;
import com.java.system.common.msg.Result;
import com.java.system.common.msg.ResultGenerator;
import com.java.system.common.service.BaseService;
import com.java.system.common.validatedGroup.Add;
import com.java.system.common.validatedGroup.Modify;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: BaseSystemController
 * @Description:
 * @Version: v1.0.0
 * @Author: Shan Feiyang
 * @Date: 2019/9/6 9:38
 * Modification History:
 * Date           Author           Version     Description
 * -------------------------------------------------------------
 * 2019/9/6      Shan Feiyang        v1.0.0         创建
 */
@Slf4j
public class BaseSystemController<Service extends BaseService, Entity> {
    @Autowired
    protected Service service;

    /**
     * @return java.lang.Object
     * @Description 分页获取列表
     * @Date 2019/5/30
     * @Param [query, page, size]
     **/
   /* @GetMapping("")
    public Result page(Entity query, Integer page, Integer size) {
        PageInfo pageInfo = service.page(query, page, size);
        return ResultGenerator.genSuccessResult(RestRes.table(pageInfo));
    }*/


    /**
     * @return java.lang.Object
     * @Description 添加一条记录
     * @Date 2019/5/30
     * @Param [query]
     **/
    @PostMapping("")
    public Result add(@Validated({Add.class}) Entity entity) {
        service.insertSelective(entity);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * @return java.lang.Object
     * @Description 编辑信息
     * @Date 2019/5/30
     * @Param [query]
     **/
    @PutMapping("")
    public Result edit(@Validated({Modify.class}) Entity entity) {
        service.updateSelectiveById(entity);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * @return java.lang.Object
     * @Description 删除信息
     * @Date 2019/5/30
     * @Param [id]
     **/
    @DeleteMapping("/{id}")
    public Result del(@PathVariable String id) {
        service.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    /**
     * @param id description
     * @return java.lang.Object
     * @Description 查询单条记录
     * @author yangyuwei on 2019/8/20 19:58
     **/
    @GetMapping("/{id}")
    public Result get(@PathVariable String id) {
        return ResultGenerator.genSuccessResult(service.selectById(id));
    }
}
